package org.blog.service;

import java.util.List;

import org.blog.dao.CommentDao;
import org.blog.helper.Constants;
import org.blog.helper.Result;
import org.blog.model.ActionType;
import org.blog.model.ModelComment;
import org.blog.model.ModelPassport;
import org.blog.service.util.ServiceConstants;
import org.blog.service.util.ValidateHelper;
import org.springframework.beans.factory.annotation.Autowired;

public class CommentServiceImpl implements CommentService {

	@Autowired
	private CommentDao commentDao;

	@Override
	public Result<?> addUserComment(ModelComment t, ModelPassport passport) {
		if (passport != null && passport.getRole() >= ServiceConstants.COMMENTS) {
			if (ValidateHelper.validate(t, ActionType.MODIFY)) {
				t.setUsername(passport.getUsername());
				t.setAction(1);
				t.setActionTime(System.currentTimeMillis() / 1000);
				t.setCreateTime(System.currentTimeMillis() / 1000);
				commentDao.addUserComment(t);
				return new Result<Object>(200);
			} else {
				return new Result<Object>(400);
			}
		} else {
			return new Result<Object>(401);
		}
	}

	@Override
	public Result<?> deleteUserComment(ModelComment t, ModelPassport passport) {
		if (passport != null && passport.getRole() >= ServiceConstants.COMMENTS) {
			if (ValidateHelper.validate(t, ActionType.DELETE)) {
				t.setUsername(passport.getUsername());
				t.setAction(3);
				t.setActionTime(System.currentTimeMillis() / 1000);
				commentDao.deleteUserComment(t);
				return new Result<Object>(200);
			} else {
				return new Result<Object>(400);
			}
		} else {
			return new Result<Object>(401);
		}
	}

	public Result<?> deleteAdminComment(ModelComment t, ModelPassport passport) {
		if (passport != null && passport.getRole() >= ServiceConstants.ADMIN) {
			if (ValidateHelper.validate(t, ActionType.DELETE)) {
				t.setAction(3);
				t.setActionTime(System.currentTimeMillis() / 1000);
				commentDao.deleteAdminComment(t);
				return new Result<Object>(200);
			} else {
				return new Result<Object>(400);
			}
		} else {
			return new Result<Object>(401);
		}
	}

	@Override
	public Result<?> modifyUserComment(ModelComment t, ModelPassport passport) {
		if (passport != null && passport.getRole() >= ServiceConstants.COMMENTS) {
			if (ValidateHelper.validate(t, ActionType.MODIFY)) {
				t.setUsername(passport.getUsername());
				t.setAction(2);
				t.setActionTime(System.currentTimeMillis() / 1000);
				commentDao.modifyUserComment(t);
				return new Result<Object>(200);
			} else {
				return new Result<Object>(400);
			}
		} else {
			return new Result<Object>(401);
		}
	}

	@Override
	public Result<ModelComment> getComment(ModelComment t) {
		if (ValidateHelper.validate(t, ActionType.GET)) {
			ModelComment comment = commentDao.getComment(t.getId());
			return new Result<ModelComment>(comment, 200);
		} else {
			return new Result<ModelComment>(400);
		}
	}

	@Override
	public Result<List<ModelComment>> getUserCommentList(Integer page,
			ModelPassport passport) {
		if (passport != null && passport.getRole() >= ServiceConstants.COMMENTS) {
			if (page != null) {
				int start = (page - 1) * Constants.pages;
				int end = page * Constants.pages;
				List<ModelComment> list = commentDao.getUserCommentList(
						passport.getUsername(), start, end);
				return new Result<List<ModelComment>>(list, 200);
			} else {
				return new Result<List<ModelComment>>(400);
			}

		} else {
			return new Result<List<ModelComment>>(401);
		}
	}

	@Override
	public Result<List<ModelComment>> getArticleCommentList(ModelComment t) {
		if (ValidateHelper.validate(t, ActionType.GET_PARENT)) {
			List<ModelComment> list = commentDao
					.getArticleCommentList(t.getArtideId());
			return new Result<List<ModelComment>>(list, 200);
		} else {
			return new Result<List<ModelComment>>(400);
		}
	}

	@Override
	public Result<List<ModelComment>> getAdminCommentList(Integer page,
			ModelPassport passport) {

		if (passport != null && passport.getRole() >= ServiceConstants.ADMIN) {
			if (page != null) {
				int start = (page - 1) * Constants.pages;
				int end = page * Constants.pages;
				List<ModelComment> list=commentDao.getAdminCommentList(start,end);
				return new Result<List<ModelComment>>(list,200);
			} else {
				return new Result<List<ModelComment>>(400);
			}
		} else {
			return new Result<List<ModelComment>>(401);
		}

	}

}
